using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


namespace CreamX.Framework.Game
{
    public class CreamXAnimatedObject : CreamXObject
    {
        private AnimationInfo _animation;
        float _TotalElapsed = 0;

        public CreamXAnimatedObject()
        {
        }

        new public float DisplayWidth
        {
            get
            {
                if(_animation!=null)
                    return _animation.SizeX * Scale;
                else
                   return  Material.Texture.Width * Scale;
            }
        }

        new public float DisplayHeight
        {
            get
            {
                if(_animation!=null)
                    return _animation.SizeY * Scale;
                else
                   return Material.Texture.Height* Scale;
            }
        }

        public float Scale
        {
            get { return _scale; }
            set
            {
                _scale = value;
                if (_positionItem != null)
                    _positionItem.Size = new Vector2(DisplayWidth, DisplayHeight);
                if (_isRegistered)
                {
                    _boundingSphere.Radius = _boundingSphere.Radius = (float)DisplayWidth / 2;
                }

            }
        }

        public AnimationInfo Animation
        {
            get { return _animation; }
            set { _animation = value; }
        }

        public override object Clone()
        {
            //CreamXObject obj= base.Clone() as CreamXObject;
            CreamXAnimatedObject __an = new CreamXAnimatedObject();
            this.CopyTo(__an);

            __an.Animation = (AnimationInfo)Animation.Clone();
            //__an.ObjectType.AddRange(ObjectType);
            return __an;
        }

        public override void OnRegister()
        {
            base.OnRegister();
            //Origin = new Vector2(Animation.SizeX / 2, Animation.SizeY/ 2);
            _boundingSphere = new BoundingSphere();
            _boundingSphere.Center = new Vector3(Position, 0);
            _boundingSphere.Radius = (float)Animation.SizeX / 2;
        }

        public override void Draw(SpriteBatch spritebatch)
        {
            Vector2 position;

            if (IgnoreCameraPosition)
                position = Position;
            else
                position = Scene.Camera.GetRenderPosition(Position);
           
            if (FlipX)
            {
                //Scene.Camera.GetRenderScale(Scale)
                spritebatch.Draw(Material.Texture, position, GetSourcRectForFrame(), Color, Rotation,
                    new Vector2(Animation.SizeX / 2, Animation.SizeY / 2), Scene.Camera.GetRenderScale(Scale), SpriteEffects.FlipHorizontally, Layer / 10);
            }
            if (FlipY)
            {
                spritebatch.Draw(Material.Texture, position, GetSourcRectForFrame(), Color, Rotation,
                    new Vector2(Animation.SizeX / 2, Animation.SizeY / 2), Scene.Camera.GetRenderScale(Scale), SpriteEffects.FlipVertically, Layer / 10);
            }
            else
            {
                //Color coll = new Color(0, 0, 0, 60);
                //ShadowOffset.X += ((Material.Texture.Width *0.1f) * Scale ) -10;
                //spritebatch.Draw((Texture2D)Material.Texture, position +ShadowOffset, null, coll, 0,
                //   Origin,new Vector2(Scene.Camera.GetRenderScale(Scale)*1.15f,
                //   Scene.Camera.GetRenderScale(Scale)*1.1f),
                //   SpriteEffects.None, Layer / 10);

                spritebatch.Draw(Material.Texture, position,
                                GetSourcRectForFrame(), Color, Rotation,
                                    Origin,
                                    Scene.Camera.GetRenderScale(Scale),
                                    SpriteEffects.None, Layer / 10);

            }



        }

        public Rectangle GetSourcRectForFrame()
        {
            if (_animation != null && _animation.TileCols > 0 && _animation.TileRows > 0)
            {

                int _currentFrame = 0;
                if (_animation._isInOrder)
                {
                    _currentFrame = _animation.CurrentFrame;
                }
                else
                    _currentFrame = _animation.Frames[_animation.CurrentFrame];
                // Extract the right area of the sprite for the given frame.
                int x = _currentFrame % _animation.TileCols;
                int y = _currentFrame / _animation.TileCols;
                Rectangle srcrect = new Rectangle(x * _animation.SizeX, y * _animation.SizeY,
                    _animation.SizeX, _animation.SizeY);

                // Create a SpriteItem object and return it to the caller.
                return srcrect;
            }
            return Rectangle.Empty;
        }

        public override void Update(float gameTime)
        {
            base.Update(gameTime);
            if (Animation == null)
                return;
            if (Animation.IsRunning)
            {

                _TotalElapsed += gameTime;// (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (!Animation.Reverse && Animation.CurrentFrame < Animation.EndFrame)
                {
                    if (_TotalElapsed > (float)1 / (float)Animation.AnimationSpeed)
                    {
                        Animation.CurrentFrame++;

                        //Animation.CurrentFrame = Animation.CurrentFrame % Animation.FrameCount;
                        _TotalElapsed = 0;
                    }
                }
                else if (Animation.Reverse && Animation.CurrentFrame > Animation.StartFrame)
                {
                    if (_TotalElapsed > (float)1 / (float)Animation.AnimationSpeed)
                    {
                        Animation.CurrentFrame--;

                        //Animation.CurrentFrame = Animation.CurrentFrame % Animation.FrameCount;
                        _TotalElapsed = 0;
                    }
                }
                else
                {
                    if (!Animation.Loop)
                    {
                        Animation.IsRunning = false;
                        if (Animation.AnimationAfter != null)
                        {
                            Animation = Animation.AnimationAfter;
                            ResetAnimation();
                            StartAnimation();
                            _TotalElapsed = 0;
                        }
                    }
                    else
                        ResetAnimation();

                    if (AnimationEnded != null)
                        AnimationEnded(Animation, EventArgs.Empty);
                }
            }

        }

        public event EventHandler AnimationEnded;

        public void StartAnimation()
        {
            Animation.IsRunning = true;
        }

        public void StopAnimation()
        {
            Animation.IsRunning = false;
        }

        public void ResetAnimation()
        {
            if (Animation.Reverse)
                Animation.CurrentFrame = Animation.EndFrame;
            else
                Animation.CurrentFrame = Animation.StartFrame;
        }

        
    }
}
